package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.app.x.CommonCode;
import easier.app.x.task.QueueAction;
import easier.recycler.DataViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MainActivity61 extends AppCompatActivity {

    private RecyclerView mRecycler;

    private final Random mRandom = new Random();
    private QueueAction< Data > mAction = new QueueAction61();
    private List< Data > mDataList;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity61.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main61 );
        initView();
    }

    private void initView() {

        mRecycler = (RecyclerView) findViewById( R.id.recycler );
        mRecycler.setLayoutManager( new LinearLayoutManager( this ) );
        mRecycler.setAdapter( new MultiTypeAdapter( convert() ) );
    }

    private List< TypeViewHolder > convert() {

        mDataList = new ArrayList<>();
        for ( int i = 0; i < 100; i++ ) {
            Data e = new Data();
            e.title = "Flutter " + i;
            e.content = "Flutter makes it easy and fast to build beautiful apps for mobile and beyond.";
            mDataList.add( e );
        }

        List< TypeViewHolder > list = new ArrayList<>();
        for ( Data data : mDataList ) {
            list.add( build( data ) );
        }

        return list;
    }

    private TypeViewHolder build( Data data ) {

        return new DataViewHolder< Data >( R.layout.activity_main51_item, data ) {
            @Override
            protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                super.onBind( viewHolder, root, position, type, payloads );

                ((TextView) root.findViewById( R.id.name_text )).setText( data.title );
                ((TextView) root.findViewById( R.id.description_text )).setText( data.content );
                ((TextView) root.findViewById( R.id.star_count_text )).setText( data.star );

                if ( TextUtils.isEmpty( data.star ) ) {
                    mAction.add( data );
                }
            }

            @Override
            protected void onUnBind( ViewHolder viewHolder, View root, int position ) {

                super.onUnBind( viewHolder, root, position );
                mAction.remove( data );
            }

            @Override
            public boolean itemEquals( @Nullable TypeViewHolder obj ) {

                return Objects.equals( getElement().title, ((DataViewHolder< Data >) obj).getElement().title );
            }

            @Override
            public boolean contentEquals( @Nullable TypeViewHolder obj ) {

                return Objects.equals( getElement(), ((DataViewHolder< ? >) obj).getElement() );
            }
        };
    }

    private static class Data {

        private String title;
        private String content;
        private String star;

        @Override
        public boolean equals( Object o ) {

            if ( this == o ) {
                return true;
            }
            if ( o == null || getClass() != o.getClass() ) {
                return false;
            }

            Data data = (Data) o;

            return Objects.equals( title, data.title ) && Objects.equals( star, data.star );
        }

        @Override
        public int hashCode() {

            return title != null ? title.hashCode() : 0;
        }
    }

    private class QueueAction61 extends QueueAction< Data > {

        @Override
        protected void onAccept( Data data ) {

            Observable.just( data )
                .delay( 1, TimeUnit.SECONDS )
                .observeOn( AndroidSchedulers.mainThread() )
                .subscribe( new Observer< Data >() {
                    @Override
                    public void onSubscribe( @NonNull Disposable d ) {

                    }

                    @Override
                    public void onNext( @NonNull Data data ) {

                        int i = mDataList.indexOf( data );
                        data.star = String.valueOf( i );

                        CommonCode.notifyItemChanged( mRecycler, i );

                        loop();
                    }

                    @Override
                    public void onError( @NonNull Throwable e ) {

                    }

                    @Override
                    public void onComplete() {

                    }
                } );
        }

        @Override
        protected int configLoopTime() {

            return 3;
        }
    }
}